package com.hnevc.trafficdemotest1.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hnevc.trafficdemotest1.R;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {
    String urls = "http://192.168.1.142:8080/RestaurantServer/user/doLogin";
    private static final String TAG = "LoginActivity";
    RequestQueue requestQueue;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    EditText mTvUserName, mTvPassword;
    CheckBox checkBox, checkBox1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mTvUserName = (EditText) findViewById(R.id.id_et_username);
        mTvPassword = (EditText) findViewById(R.id.id_et_password);
        checkBox = (CheckBox) findViewById(R.id.rember_password);
        checkBox1 = (CheckBox) findViewById(R.id.auto_login_cb);
        sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
        boolean isRemember = sharedPreferences.getBoolean("rememberpassword", false);

        if (isRemember) {
            String name = sharedPreferences.getString("name", "");
            String password = sharedPreferences.getString("password", "");
            mTvUserName.setText(name);
            mTvPassword.setText(password);
            checkBox.setChecked(isRemember);
        }
    }

    public void doLogin(View view) {

        JSONObject object = new JSONObject();
        try {
            object.put("userName", mTvUserName.getText().toString());
            object.put("userPassword", mTvPassword.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, urls, object,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        String result = jsonObject.optString("RESULT");
                        if (result.equals("S")) {
                            String userRole = jsonObject.optString("UserRole");
                            rememberUserNamePassword();
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("Role",userRole);
                            editor.commit();
                            Toast.makeText(LoginActivity.this, jsonObject.optString("ERRMSG") + ",角色为:" + userRole, Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else if (result.equals("F")) {
                            Toast.makeText(LoginActivity.this, jsonObject.optString("ERRMSG"), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(LoginActivity.this, "登录错误", Toast.LENGTH_SHORT).show();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e(TAG, "onErrorResponse: " + volleyError.toString());
                        Toast.makeText(LoginActivity.this, "登录错误", Toast.LENGTH_SHORT).show();
                    }
                });
        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonObjectRequest);
    }
    private void rememberUserNamePassword() {
       SharedPreferences.Editor editor = sharedPreferences.edit();
        if (checkBox.isChecked()) {
            editor.putBoolean("rememberpassword", true);
            editor.putString("name", mTvUserName.getText().toString());
            editor.putString("password", mTvPassword.getText().toString());
        } else {
            editor.clear();
        }
        editor.commit();
    }
}
